<?php namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Input;
use App\Api\Models\WebsiteFeedback;
use Log;
class SystemController extends Controller{
    
    public function qiniuCallback(){
        $info = Input::all();
        $returnInfo['statusCode'] = 0;
        $returnInfo['message'] = '成功';
        $returnInfo['timestamp'] = time();
        $returnInfo['data'] = $info;
        return json_encode($returnInfo, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }
   
    public function websiteFeedBack(){
        header("Access-Control-Allow-Origin: *");
        $name = Input::get('name');
        $email = Input::get('email');
        $phone = Input::get('phone');
        $content = Input::get('content');
        $realip = '';
        if(isset($_SERVER)){    
            if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
                $realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            }elseif(isset($_SERVER['HTTP_CLIENT_IP'])) {
                $realip = $_SERVER['HTTP_CLIENT_IP'];
            }else{
                $realip = $_SERVER['REMOTE_ADDR'];
            }
        }
        $websiteFeedback =new WebsiteFeedback();
        $websiteFeedback->name = $name;
        $websiteFeedback->email = $email;
        $websiteFeedback->phone = $phone;
        $websiteFeedback->content = $content;
        $websiteFeedback->ua = $realip.'-'.$_SERVER['HTTP_USER_AGENT'];
        $websiteFeedback->save();
        $returnInfo['statusCode'] = 0;
        $returnInfo['message'] = '成功';
        $returnInfo['timestamp'] = time();
        return json_encode($returnInfo, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }
    
    public function readLogs() { 
        Input::get('file');
        $info['page'] = Input::get('page',1);
        $info['day'] = Input::get('day',0000000);
        $info['file'] = Input::get('file');
        //读取当日日志
        //发送get请求
        if ($info['page'] < 1) {
            return 'Error page';
        }
        if ($info['file'] == 'workman') {
            $filename = __DIR__ . '/../../Service/workman/logs/workman.log';
            if (!file_exists($filename)) {
                echo $filename . '日志文件不存在';
                Log::info($filename . '日志文件不存在');
                return;
            }
        } else {
            $filename = __DIR__ . '/../../../../storage/logs/laravel-' . date("Y-m-d", time() - $info['day'] * 24 * 60 * 60) . '.log';
            if (!file_exists($filename)) {
                echo $filename . '日志文件不存在';
                Log::info($filename . '日志文件不存在');
                return;
            }
        }
        $fp = fopen($filename, "r");
        $totalPage = (int) (filesize($filename) / 8192 + 1);
        if ($totalPage < $info['page']) {
            $info['page'] = $totalPage;
        }
        fseek($fp, -8192 * $info['page'], SEEK_END);
        $data = fread($fp, 8192);
        $data = str_replace(' #', '<br>#', $data);
        echo '<pre>';
        echo '<h2>日期：' . date("Y-m-d", time() - $info['day'] * 24 * 60 * 60) . '总共： ' . $totalPage . '页，第' . $info['page'] . '页<br/></h2>';
        echo $data;
    }
    
}